package com.loveto.service;

import com.loveto.common.ImageDownloader;
import com.loveto.constant.LovetoConstant;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class UserinfoViewAlbumOthersActivity extends BaseActivity implements
View.OnClickListener{
	
	// 사용하는 변수들
	private SharedPreferences pref;
	private ProgressDialog progressDialog;
	private Handler myHandler = new Handler();	
	private Intent intentMoveToPage;
	private ImageView v_photo_01;
	private ImageView v_photo_02;
	private ImageView v_photo_03;
	private ImageView v_photo_04;
	private String memberid;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loveto_userinfo_view_album_others);
		
		Log.d("tag","앨범수정시작"); 
		//변수 선언
		progressDialog = new ProgressDialog(this);
		v_photo_01 = (ImageView)findViewById(R.id.photo_01);
		v_photo_02 = (ImageView)findViewById(R.id.photo_02);
		v_photo_03 = (ImageView)findViewById(R.id.photo_03);
		v_photo_04 = (ImageView)findViewById(R.id.photo_04);
		
		//로컬에서 정보 받아오기
		pref = getSharedPreferences("userInfo", Activity.MODE_PRIVATE);
		
		//전 엑티비티에서 정보 받아오기
		Intent intent = getIntent();
		memberid = intent.getStringExtra("memberid");
		
		// 클릭 리스너 달기 // 버튼
		RelativeLayout v_btn_goto_back = (RelativeLayout)findViewById(R.id.btn_goto_back);
		v_btn_goto_back.setOnClickListener(this);
		v_photo_01.setOnClickListener(this);
		v_photo_02.setOnClickListener(this);
		v_photo_03.setOnClickListener(this);
		v_photo_04.setOnClickListener(this);
		
		//시작 시 서버에서 사진 받아오기 
		if (isNetworkAvailableAndNotice()) {
			getPhotoFromServer();
		}
		
	}
	
	
	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.photo_01:
			String photo_url1_yn = pref.getString("looking_photo_url1", "");
			if ("Y".equals(photo_url1_yn)) { //사진이 있으면
				//사진 보러가기
				intentMoveToPage = new Intent(UserinfoViewAlbumOthersActivity.this,UserinfoViewPhotoActivity.class);
				intentMoveToPage.putExtra("memberid", memberid);
				intentMoveToPage.putExtra("photonum", 1);
				startActivity(intentMoveToPage);
			}
			break;
		case R.id.photo_02:
			String photo_url2_yn = pref.getString("looking_photo_url2", "");
			if ("Y".equals(photo_url2_yn)) { //사진이 있으면
				//사진 보러가기
				intentMoveToPage = new Intent(UserinfoViewAlbumOthersActivity.this,UserinfoViewPhotoActivity.class);
				intentMoveToPage.putExtra("memberid", memberid);
				intentMoveToPage.putExtra("photonum", 2);
				startActivity(intentMoveToPage);
			}
			break;
		case R.id.photo_03:
			String photo_url3_yn = pref.getString("looking_photo_url3", "");
			if ("Y".equals(photo_url3_yn)) { //사진이 있으면
				//사진 보러가기
				intentMoveToPage = new Intent(UserinfoViewAlbumOthersActivity.this,UserinfoViewPhotoActivity.class);
				intentMoveToPage.putExtra("memberid", memberid);
				intentMoveToPage.putExtra("photonum", 3);
				startActivity(intentMoveToPage);
			}
			break;
		case R.id.photo_04:
			String photo_url4_yn = pref.getString("looking_photo_url4", "");
			if ("Y".equals(photo_url4_yn)) { //사진이 있으면
				//사진 보러가기
				intentMoveToPage = new Intent(UserinfoViewAlbumOthersActivity.this,UserinfoViewPhotoActivity.class);
				intentMoveToPage.putExtra("memberid", memberid);
				intentMoveToPage.putExtra("photonum", 4);
				startActivity(intentMoveToPage);
			}
			break;
		case R.id.btn_goto_back:
			finish();
			break;
		}
	}

	   private void getPhotoFromServer() {
		   progressDialog.setMessage("사진을 불러오고있습니다.");
		   progressDialog.show();
			new Thread(new Runnable(){
				@Override
				public void run() {
					//UI 를 건들기 때문에 핸들러 사용.
					myHandler.post(new Runnable() {
						@Override
						public void run() {
					    	String photo_url1_yn = pref.getString("looking_photo_url1", "");
					    	String photo_url2_yn = pref.getString("looking_photo_url2", "");
					    	String photo_url3_yn = pref.getString("looking_photo_url3", "");
					    	String photo_url4_yn = pref.getString("looking_photo_url4", "");
					    	String photoUrl;
					    	ImageDownloader imageDownloader = new ImageDownloader();
					    	if ("Y".equals(photo_url1_yn)) {
					    		//사진 변경
					    		photoUrl = LovetoConstant.PHOTO_URL + memberid + "_1.jpg";
								imageDownloader.download(photoUrl, v_photo_01);
					    	}
					    	if ("Y".equals(photo_url2_yn)) {
					    		//사진 변경
					    		photoUrl = LovetoConstant.PHOTO_URL + memberid + "_2.jpg";
					    		imageDownloader.download(photoUrl, v_photo_02);
					    	}
					    	if ("Y".equals(photo_url3_yn)) {
					    		//사진 변경
					    		photoUrl = LovetoConstant.PHOTO_URL + memberid + "_3.jpg";
					    		imageDownloader.download(photoUrl, v_photo_03);
					    	}
					    	if ("Y".equals(photo_url4_yn)) {
					    		//사진 변경
					    		photoUrl = LovetoConstant.PHOTO_URL + memberid + "_4.jpg";
					    		imageDownloader.download(photoUrl, v_photo_04);
					    	}
					    	
					    	progressDialog.dismiss();
						}
					});
				}
			}).start();
	    }
	
}
